package com.lingyistudio.common.util;

import lombok.extern.slf4j.Slf4j;
import java.security.MessageDigest;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @ClassName: SHA1Model
 * @Description:
 * @Author: cheng
 * @Date: 2019/5/23
 */
@Slf4j
public class SHA1Model {
    private static ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
    private static char[] sha1char= {
            '!','"','#','$','%','&','(',')','*','+',
            ',','-','.','0','1','2','3','4','5','6',
            '7','8','9',':',';','<','=','>','?','@',
            'A','B','C','D','E','F','G','H','I','J',
            'K','L','M','N','O','P','Q','R','S','T',
            'U','V','W','X','Y','Z','[',']','^','`',
            'a','b','c','d','e','f','g','h','i','j',
            'k','l','m','n','o','p','q','r','s','t',
            'u','v','w','x','y','z','{','|','}','~'
    };

    //plaintext:明文 ciphertext：密文
    public static boolean verifySHA1(String plainText, String cipherText)  {
        //根据传入的密钥进行验证
        String str = cipherText.substring(40);
        String plainTextSH15 = getSHA1(plainText+str);
        if( plainTextSH15 == null ){
            plainTextSH15 = getSHA1(plainText+str);
        }
        if( cipherText.equals(plainTextSH15+str) ){
            return true;
        }
        return false;
    }

    public static String randomSHA1(String plainText)  {
        long random = threadLocalRandom.nextLong(1000000000000000L,9999999999999999L);
        String randomString =""+sha1char[(int) (random%100%(sha1char.length))]+sha1char[(int) (random/100%100%(sha1char.length))]+
                sha1char[(int) (random/10000%100%(sha1char.length))]+sha1char[(int) (random/1000000%100%(sha1char.length))]+
                sha1char[(int) (random/100000000%100%(sha1char.length))]+ sha1char[(int) (random/10000000000L%100%(sha1char.length))]+
                sha1char[(int) (random/1000000000000L%100%(sha1char.length))]+ sha1char[(int) (random/100000000000000L%100%(sha1char.length))];
        plainText=plainText+randomString;
        String ciphertext;
        ciphertext = getSHA1(plainText);
        if ( ciphertext == null ){
            ciphertext=getSHA1(plainText);
        }
        return ciphertext+randomString;
    }


    public static String getSHA1(String plaintext){
        try {
            MessageDigest sha = MessageDigest.getInstance("SHA");
            byte[] byteArray;
            byteArray = plaintext.getBytes("UTF-8");
            byte[] md5Bytes = sha.digest(byteArray);
            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            return hexValue.toString();
        } catch (Exception e) {
            System.out.println(e.toString());
            log.error(String.valueOf(e));
            e.printStackTrace();
            return null;
        }
    }
}
